package com.sgcc.fj.pmos.common.dialog;

import android.content.Context;

import android.view.View;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.common.adapter.StockAdapter;
import com.sgcc.fj.pmos.common.bean.StockBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 历史电量列表弹窗
 */
public class StockPopView extends CenterPopupView {
    RecyclerView recyclerView;
    Map<String, Double> map;
    Context context;
    List<StockBean> list = new ArrayList<>();

    public StockPopView(Context context, Map<String, Double> map) {
        super(context);
        this.map = map;
        this.context = context;
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.stock_pop;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        findViewById(R.id.close_img).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                StockPopView.this.dismiss();
            }
        });
        findViewById(R.id.imgLineChartClose).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                StockPopView.this.dismiss();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        StockBean stockBean;
        Set keySet = map.keySet();  //获取key集合对象

        int[] sourceArray = new int[keySet.size()];
        int i = 0;
        for (Object keyName : keySet) {
            sourceArray[i] = Integer.valueOf(keyName + "");
            i++;
        }
        try {
            int[] sortList;
            sortList = sort(sourceArray);
            for (int time : sortList) {
            stockBean = new StockBean();
            stockBean.setTime(time+"");
            stockBean.setPower((double)map.get(time+""));
            list.add(stockBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        recyclerView.setAdapter(new StockAdapter(list));
    }

    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝，不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        for (int i = 1; i < arr.length; i++) {
            // 设定一个标记，若为true，则表示此次循环没有进行交换，也就是待排序列已经有序，排序已经完成。
            boolean flag = true;

            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;

                    flag = false;
                }
            }

            if (flag) {
                break;
            }
        }
        return arr;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }


    @Override
    protected int getPopupWidth() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .5f);
    }


    public interface OnListener {
        /**
         * 点击确定按钮事件
         */
        void onPositiveClick(StockPopView pop, boolean ischeck);
    }


}